import { login, logout } from '@/api/login'
import { setStorage, removeStorage } from '@/storage/index'
import { ACCESS_TOKEN } from '../types'
import { USER } from '../mutations_types'
export default {
	state: {
		token: '',
	},
	getters: {},
	mutations: {
		[USER.SET_TOKEN](state, token) {
			state.token = token
		},
	},
	actions: {
		Login({ commit }, userInfo) {
			return new Promise((resolve, reject) => {
				login(userInfo)
					.then((response) => {
						const token = response.data
						setStorage(ACCESS_TOKEN, token)
						commit.SET_TOKEN(token)
						resolve()
					})
					.catch((e) => {
						reject(e)
					})
			})
		},
		Logout({ commit, state }) {
			return new Promise((resolve, reject) => {
				logout(state.token)
					.then((resolve) => {
						commit('SET_TOKEN', '')
						removeStorage(ACCESS_TOKEN)
						resolve()
					})
					.catch((e) => {
						reject(e)
					})
			})
		},
	},
}
